package com.itheima.web;

import com.itheima.dao.StaticsVoDao;
import com.itheima.pojo.StaticsVo;
import com.itheima.utils.FindDatesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/statics")
public class StaticsController {
    @Autowired
    StaticsVoDao staticsVoDao;

    @GetMapping("/dishCash")
    public Map dishCategoryCash(){
        List<StaticsVo> staticsVoList = staticsVoDao.selectDishCash();
        Map map = new HashMap<>();
        map.put("dishCategoryCash",staticsVoList);
        return map;
    }

    @GetMapping("/setmealCash")
    public Map setmealCash(){
        List<StaticsVo> staticsVoList = staticsVoDao.selectSetmealCash();
        Map map = new HashMap<>();
        map.put("setmealCash",staticsVoList);
        return map;
    }

    @GetMapping("/dishNum")
    public Map dishNum(){
        List<StaticsVo> staticsVoList = staticsVoDao.selectDishNum();
        Map map = new HashMap<>();
        map.put("dishNum",staticsVoList);
        return map;
    }

    @GetMapping("/setmealNum")
    public Map setmealNum(){
        List<StaticsVo> staticsVoList = staticsVoDao.selectSetmealNum();
        Map map = new HashMap<>();
        map.put("setmealNum",staticsVoList);
        return map;
    }

    @GetMapping("/cashPerMonth")
    public Map cashPerMonth() throws ParseException {
        List<Map<String,Object>> cashPerMonth = staticsVoDao.selectCashPerMonth();
        LocalDate nowDate = LocalDate.now();
        LocalDate beforeDate = LocalDate.now().minusYears(1);
        List<String> timelist = FindDatesUtils.findDates( beforeDate.toString(),nowDate.toString());
        List<Integer> datalist = new ArrayList<>();
        for (String time : timelist) {
            Optional<Map<String, Object>> optional = cashPerMonth.stream().filter(item -> item.get("monthlist").equals(time)).findFirst();
            if(optional.isPresent()) {
                //存在销售额，将销售额存储到 售额集合中
                BigDecimal dataList1 = (BigDecimal) optional.get().get("datalist");
                datalist.add(dataList1.intValue());
            }else {
                //不存在销售额，将销售额存储到 销售额集合中dataList.add(0):
                datalist.add(0);
            }
        }
        Map map = new HashMap<>();
        map.put("monthlist",timelist);
        map.put("datalist",datalist);
        return map;
    }
}
